﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.SoCoSinhVienService;
namespace eTMS.Controllers{
public class SoCoSinhVienControllerBase : CommonController{
	public SoCoSinhVienControllerBase()
		: base()
	{	
		this.dao = new SoCoSinhVienServiceClient();
	}
	~SoCoSinhVienControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private SoCoSinhVienEntity oSoCoSinhVien;	
	public SoCoSinhVienEntity SoCoSinhVienObject{
		get{
			return oSoCoSinhVien;
		}
		set{
			oSoCoSinhVien=value;
		}
	}
	protected SoCoSinhVienServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSSoCoSinhVien(false);			
			return _Table;
		}
	}
	public void LoadAllDSSoCoSinhVien(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oSoCoSinhVien==null)return false;
		bool result=true;
		try{
			oSoCoSinhVien = dao.SaveRet(oSoCoSinhVien);
		}
		catch{
			result=false;
		}
		if(result){
			_ListSoCoSinhVien.Add(oSoCoSinhVien);
		}
		return result;
	}
	public bool Update(){
		if(oSoCoSinhVien==null)return false;
		return dao.Update(oSoCoSinhVien);
	}
	public bool Delete(){
		if(oSoCoSinhVien==null)return false;
		bool retvalue = dao.Delete(oSoCoSinhVien);
		if(retvalue){			
			_ListSoCoSinhVien.Remove(oSoCoSinhVien);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 SoCoSinhVien_ID){
		bool retvalue = dao.DeleteByID(SoCoSinhVien_ID);		
		if(retvalue){
			SoCoSinhVienEntity oDeleted=FindSoCoSinhVien(SoCoSinhVien_ID);
			if(oDeleted!=null){
				_ListSoCoSinhVien.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oSoCoSinhVien = new SoCoSinhVienEntity();
	}
    //Copy data from data row in grid to object
    public SoCoSinhVienEntity MapDataRowToObject(DataRow row){
		SoCoSinhVienEntity o = new SoCoSinhVienEntity();
		 o.GhiChu=Convert.ToString(row["GhiChu"]);
		 if(!row.IsNull("HocKy_ID"))
			o.HocKy_ID=ConvertInt(row["HocKy_ID"]);
		 if(!row.IsNull("NamHoc_ID"))
			o.NamHoc_ID=ConvertInt(row["NamHoc_ID"]);
		 if(!row.IsNull("SinhVien_ID"))
			o.SinhVien_ID=(Int32)(row["SinhVien_ID"]);
		 if(!row.IsNull("SoCoSinhVien_ID"))
		    o.SoCoSinhVien_ID=(Int32)(row["SoCoSinhVien_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(SoCoSinhVienEntity o,DataRow row){
		row["GhiChu"]=o.GhiChu;
		row["HocKy_ID"]=o.HocKy_ID;
		row["NamHoc_ID"]=o.NamHoc_ID;
		row["SinhVien_ID"]=o.SinhVien_ID;
		row["SoCoSinhVien_ID"]=o.SoCoSinhVien_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.SoCoSinhVienObject,row);
    }
	public void LoadObject(Int32 SoCoSinhVien_ID){
		SoCoSinhVienEntity oLoad=FindSoCoSinhVien(SoCoSinhVien_ID);
		if(oLoad==null){
			oSoCoSinhVien=this.LoadSoCoSinhVien(SoCoSinhVien_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oSoCoSinhVien.IsNullSoCoSinhVien_ID;
			if(!bNotIsNull){
				this.ListSoCoSinhVien.Add(oSoCoSinhVien);
			}
		}
		else{
			oSoCoSinhVien=oLoad;
		}
	}
	#region CachedList SoCoSinhVien
	private static List<SoCoSinhVienEntity> _ListSoCoSinhVien=new List<SoCoSinhVienEntity>();
    public List<SoCoSinhVienEntity>ListSoCoSinhVien{
		get{
			return _ListSoCoSinhVien;
		}
	}
	public static List<SoCoSinhVienEntity>ListSoCoSinhVienStatic{
		get{
			return _ListSoCoSinhVien;
		}
	}
	public SoCoSinhVienEntity FindSoCoSinhVien(Int32 SoCoSinhVien_ID){
		SoCoSinhVienEntity oResult=this.ListSoCoSinhVien.Find(delegate(SoCoSinhVienEntity oFind){
			bool result=true; 
			result&=oFind.SoCoSinhVien_ID.Equals(SoCoSinhVien_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllSoCoSinhVien(bool clearCache){
		if(_ListSoCoSinhVien.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		SoCoSinhVienServiceClient dao = new SoCoSinhVienServiceClient();
		_ListSoCoSinhVien=new List<SoCoSinhVienEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public SoCoSinhVienEntity LoadSoCoSinhVien(Int32 SoCoSinhVien_ID){			
		SoCoSinhVienEntity o=dao.SelectOne(SoCoSinhVien_ID);		
		return o;
	}
	#endregion
}
}
